package com.ol.edu.server.controller.advertise;


import com.github.pagehelper.PageInfo;
import com.ol.edu.server.entity.PromotionAd;
import com.ol.edu.server.entity.ResponseResult;
import com.ol.edu.server.service.advertise.PromotionAdService;
import com.ol.edu.server.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author Roger
 * @date 2020/9/7 10:58
 * @description
 */
@RestController
@RequestMapping("/PromotionAd")
@CrossOrigin
public class PromotionAdController {

    @Autowired
    private PromotionAdService promotionAdService;

    /*
        分页获取广告列表数据
     */
    @RequestMapping("/findAllPromotionAdByPage")
    public ResponseResult findAllPromotionAdByPage(Integer currentPage, Integer pageSize) {
        PageInfo pageInfo = promotionAdService.findAllPromotionAdByPage(currentPage, pageSize);
        return new ResponseResult(true, 200, "广告查询成功", pageInfo);
    }

    /*
        图片上传
     */
    @RequestMapping("/PromotionAdUpload")
    public ResponseResult promotionAdUpload(@RequestBody @RequestParam("file") MultipartFile file) {
        Map map = UploadUtils.upload(file);
        return new ResponseResult(true, 200, "上传成功", map);
    }

    /*
        新建&修改广告接口
     */
    @RequestMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd) {
        // 传入的参数如果没有ID就是新增操作
        if (promotionAd.getId() == null) {
            promotionAdService.savePromotionAd(promotionAd);
            return new ResponseResult(true, 200, "添加广告成功", null);
        } else {// 更新
            promotionAdService.updatePromotion(promotionAd);
            return new ResponseResult(true, 200, "修改广告成功", null);
        }
    }

    /*
        接收广告ID,返回广告详细信息
     */
    @RequestMapping("/findPromotionAdById")
    public ResponseResult findPromotionAdById(Integer id) {
        PromotionAd promotionAd = promotionAdService.findPromotionAdById(id);

        return new ResponseResult(true, 200, "查询成功", promotionAd);
    }

    /*
        修改广告上下线状态
     */
    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(PromotionAd promotionAd) {
        Map map = promotionAdService.updatePromotionAdStatus(promotionAd);
        return new ResponseResult(true, 200, "修改成功", map);
    }


}
